package com.wu.dms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.convertion.errorcode.ListSongErrorCode;
import com.wu.common.convertion.exception.ServiceException;
import com.wu.dms.dao.entity.ListSong;
import com.wu.dms.dao.mapper.ListSongMapper;
import com.wu.dms.dto.ListSongRequestDTO;
import com.wu.dms.service.ListSongService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Cousin
 */
@RequiredArgsConstructor
@Service
public class ListSongServiceImpl extends ServiceImpl<ListSongMapper, ListSong> implements ListSongService {





    @Override

    public void updateListSongMsg(ListSongRequestDTO updateListSongRequestDTO) {

        ListSong listSong = BeanUtil.toBean(updateListSongRequestDTO, ListSong.class);
        if (!updateById(listSong)) {
            throw new ServiceException(ListSongErrorCode.LIST_SONG_UPDATE_ERROR);
        }
    }

    @Override
    public void deleteListSong(Integer songId) {
        LambdaQueryWrapper<ListSong> lambdaQueryWrapper = Wrappers.lambdaQuery(ListSong.class)
                .eq(ListSong::getSongId, songId);
        if (!remove(lambdaQueryWrapper)) {
            throw new ServiceException(ListSongErrorCode.LIST_SONG_DELETE_ERROR);
        }

    }

    @Override
    public void addListSong(ListSongRequestDTO addListSongRequestDTO) {

        ListSong listSong = BeanUtil.toBean(addListSongRequestDTO, ListSong.class);
        if (!updateById(listSong)) {
            throw new ServiceException(ListSongErrorCode.LIST_SONG_SAVE_ERROR);
        }

    }

    @Override

    public List<ListSong> listSongOfSongId(Integer songListId) {

        return query().eq("song_list_id", songListId).list();
    }

}
